// import { Message } from 'element-ui'

const readyState = {
  cfId: '', // 房间id
  cfCode: '', // 会议号
  cfName: '', // 会议名称
  cfSponsorid: '', // 会议发起人
  cfSponsorName: '', // 会议发起人姓名
  ws: null, // websocket实例化对象
  wsAddress: '', // ws地址
  peerList: {}, // peer数组
  localStream: null, // 本地流
  credential: '', // ice-server密码
  username: '', // ice-server用户名
  iceServer: [], // ice-server地址（stun...)
  userList: [], // 会议室用户数组
  showMemberOperation: false, // 是否展示对成员的操作
  fileList: [], // 会议室文件列表
  showExit: false, // 是否展示确认退出会议提示框
  cfTime: '', // 会议开始时间
  settings: {
    // 入会设置
    micro: true,
    camera: true
  }
}
// 深拷贝
const state = JSON.parse(JSON.stringify(readyState))

const mutations = {
  // 设置房间信息
  set_roomInfo (state, data) {
    state.cfId = data.cfId
    state.cfCode = data.cfCode
    state.cfName = data.cfName
    state.cfSponsorid = data.cfSponsorid + ''
    state.cfSponsorName = data.cfSponsor
  },
  // 设置ws实例化对象
  set_ws (state, obj) {
    state.ws = obj
  },
  // 设置ws地址
  set_wsAddress (state, address) {
    state.wsAddress = address
  },
  // 设置peer实例化对象
  set_peerList (state, { peer, userId }) {
    state.peerList[userId] = peer
  },
  // 设置localStream
  set_localStream (state, stream) {
    state.localStream = stream
  },
  // 设置localVolume
  set_localVolume (state, volume) {
    state.localVolume = volume
  },
  // 设置ice-server密码
  set_credential (state, password) {
    state.credential = password
  },
  // 设置ice-server用户名
  set_username (state, username) {
    state.username = username
  },
  // 设置ice-server地址
  set_iceServer (state, iceServer) {
    state.iceServer = iceServer
  },
  // 设置会议室用户数组
  set_userList (state, list) {
    list.forEach((item) => {
      item.stream = null
    })
    state.userList = list
  },
  // 设置是否展示对成员的操作
  set_showMemberOperation (state, status) {
    state.showMemberOperation = status
  },
  // 新增会议室用户
  add_user (state, user) {
    user.stream = null
    state.userList.push(user)
  },
  // 删除会议室用户
  delete_user (state, id) {
    state.userList = state.userList.filter((item) => item.id !== id)
    delete state.peerList['peer' + id]
  },
  // 设置会议室用户视频
  set_user_stream (state, { id, stream }) {
    state.userList.forEach((item) => {
      if (item.id === id) {
        item.stream = stream
      }
    })
  },
  // 设置会议室用户音频
  set_user_audio (state, { id, flag }) {
    state.userList.forEach((item) => {
      if (item.id === id) {
        item.micro = flag
      }
    })
  },
  // 设置会议室用户摄像头
  set_user_video (state, { id, flag }) {
    state.userList.forEach((item) => {
      if (item.id === id) {
        item.camera = flag
      }
    })
  },
  // 设置主持人转让
  set_user_host (state, id) {
    state.userList.forEach((item) => {
      if (item.id === id) {
        item.host = true
      } else if (item.host) {
        item.host = false
      }
    })
  },
  // 设置会议室文件列表
  set_fileList (state, list) {
    state.fileList = list
  },
  // 设置退出会议提示框的展示隐藏
  set_showExit (state, boolean) {
    state.showExit = boolean
  },
  // 设置会议开始时间
  set_cfTime (state, time) {
    state.cfTime = time
  },
  // 设置会议室原始状态
  set_readyState (state) {
    for (const key in state) {
      state[key] = readyState[key]
    }
  },
  // 设置入会是否开启麦克风
  set_settings_micro (state, boolean) {
    state.settings.micro = boolean
  },
  // 设置入会是否开启摄像头
  set_settings_camera (state, boolean) {
    state.settings.camera = boolean
  }
}

const actions = {

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
